﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using AjaxControlToolkit;

public partial class Admin_AddNews : System.Web.UI.Page 
{
    DataTable DTEvents;
    protected LocalFunctions FUNC;
    int iLangID;
    int CurrentPage = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin_AdminMasterPage)Master).FUNC;
        iLangID = (Session["Language"] != null) ? Convert.ToInt32(Session["Language"]) : 1;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (iLangID != Convert.ToInt32(Session["Language"]))
        {
            Session["Language"] = iLangID;
        }
        if (!IsPostBack)
        {
            imgPreview.Src = FUNC.FullAppPath + "/Gallery/" + Session["Image"];
            Session["Image"] = string.Empty;
            Session["ThumbImage"] = string.Empty;

            ddlLanguageChoose.SelectedValue = iLangID.ToString();
            
        }
        else
        {
            imgPreview.Src = FUNC.FullAppPath + "/Gallery/" + Session["image"].ToString();
            imgThumbPreview.Src = FUNC.FullAppPath + "/Gallery/" + Session["ThumbImage"].ToString();
        }
        GetEvents();
    }

    private void GetEvents()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iLangID);
        Dal.arParams.Add(CurrentPage);
        Dal.arParams.Add(20);
        DTEvents = Dal.ExecuteDBsp("RD_GetNews", Dal.arParams);
        //Session["Image"] = DTEvents.Rows[0]["imagePath"].ToString();
        //imgPreview.Src = FUNC.FullAppPath + "/Gallery/" + DTEvents.Rows[0]["imagePath"].ToString();
        dlEvents.DataSource = DTEvents;
        dlEvents.DataBind();
    }


    protected void ddlLanguageChoose_SelectedIndexChanged(object sender, EventArgs e)
    {
        iLangID = Convert.ToInt32(((DropDownList)sender).SelectedValue);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(hfEventID.Value);
            Dal.arParams.Add(iLangID); // LanguageID
            Dal.arParams.Add(txtTitle.Text); //title
            Dal.arParams.Add(Helper.ConvertStringToSql(txtSubTitle.Text)); //place
            Dal.arParams.Add(fckPageContent.Value); //description
            Dal.arParams.Add(Session["Image"]);
            Dal.arParams.Add(Session["ThumbImage"]);
            Dal.ExecuteDBsp("Admin_UpdateNews", Dal.arParams, true);
        }

        Sitemap sm = new Sitemap();
        sm.Save();
        //Clear Fields
        //hfEventID.Value = "0";
        //txtTitle.Text = string.Empty;
        //txtSubTitle.Text = string.Empty;
        //fckPageContent.Value = string.Empty;
        //Session["Image"] = string.Empty;
        //Session["ThumbImage"] = string.Empty;
        GetEvents();
    }

    protected void dlEvents_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Item.FindControl("imgDelete").ClientID.Replace("_", "$");
            cb.OnClientCancel = "onCancel";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Item.Controls.Add(cb);
        }
        catch { }
    }

    protected void btnEditEvent_Click(object sender, ImageClickEventArgs e)
    {
        int iLinkID;
        Int32.TryParse(((ImageButton)sender).CommandArgument.ToString(), out iLinkID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID);
        DataTable DT = Dal.ExecuteDBsp("Admin_GetNewsByID", Dal.arParams);

        hfEventID.Value = iLinkID.ToString();
        txtTitle.Text = DT.Rows[0]["title"].ToString();
        txtSubTitle.Text = DT.Rows[0]["subTitle"].ToString();
        fckPageContent.Value = DT.Rows[0]["Content"].ToString();

        Session["Image"] = DT.Rows[0]["imagePath"].ToString();
        Session["ThumbImage"] = DT.Rows[0]["ThumbImage"].ToString();
        imgPreview.Src = FUNC.FullAppPath + "/Gallery/" + DT.Rows[0]["imagePath"].ToString();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        hfEventID.Value = "0";
        txtTitle.Text = string.Empty;
        txtSubTitle.Text = string.Empty;
        fckPageContent.Value = string.Empty;
        Session["Image"] = string.Empty;
        Session["ThumbImage"] = string.Empty;
        imgPreview.Src = string.Empty;
    }

    protected void uplImage_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string script = string.Empty;
        string[] sExtension = e.filename.Split('.');
        string sFileName = "news_" + Helper.GenerateUniqueFileName() + "." + sExtension[sExtension.Length - 1];
        string sPath = Server.MapPath("~/Gallery/") + sFileName;
        uplImage.SaveAs(sPath);

        Session["Image"] = sFileName;
        ScriptManager.RegisterClientScriptBlock(uplImage, uplImage.GetType(), "img",
"top.document.getElementById('ctl00_ContentPlaceHolder1_imgPreview').src='" + FUNC.FullAppPath + "/Gallery/" + sFileName + "';",
true);
    }

    protected void uplThumbImage_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string script = string.Empty;
        string[] sExtension = e.filename.Split('.');
        string sFileName = "Thumb_" + Helper.GenerateUniqueFileName() + "." + sExtension[sExtension.Length - 1];
        string sPath = Server.MapPath("~/Gallery/") + sFileName;
        uplThumbImage.SaveAs(sPath);

        Session["ThumbImage"] = sFileName;
        ScriptManager.RegisterClientScriptBlock(uplThumbImage, uplThumbImage.GetType(), "img",
"top.document.getElementById('ctl00_ContentPlaceHolder1_imgThumbPreview').src='" + FUNC.FullAppPath + "/Gallery/" + sFileName + "';",
true);
    }

    protected void imgDelete_Click(object sender, ImageClickEventArgs e)
    {
        int iID;
        Int32.TryParse(((ImageButton)sender).CommandArgument.ToString(), out iID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.ExecuteDBsp("Admin_DeleteNews", Dal.arParams);
        GetEvents();
    }
}
